#!/usr/bin/env python
#coding: iso-8859-1

#Standard imports
from random import random
from pandac.PandaModules import *
from direct.actor import Actor
from direct.interval.IntervalGlobal import * 
from direct.particles.Particles import Particles 
from direct.particles.ParticleEffect import ParticleEffect 

from panda3d.ai import *

#User imports
import globals #@UnusedImport

class Enemy:
    def __init__(self):
        self.model = None
        self.time = None
        self.startPos = None
        self.stageBelong = None
        self.points = []
        self.times = []
        self.sound = None
        self.pos = None
        self.dead = False
        self.scorePoints = 0
        self.actor = Actor.Actor();
        self.node = render.attachNewNode("Enemy")
        self.loop = False

        self.idle_anim = None
        self.run_anim = None
        self.attack_anim =  None
        self.dead_anim = None
        self.hit_anim = None
        self.H = 0
        self.P = 0
        self.R= 0
        self.HPR = Vec3(0.0,0.0,0.0)
        self.scale = 1

    def init(self):
        """Init Function
        
        Initializes and runs everything related to enemy
        """
        taskMgr.doMethodLater(self.time, self._init, 'Start-Later-Enemy')

        #Creates a task that will begin according to the time imposed in xmlR

    def _init(self, task):
        """Task function


        This will be executed after the time parameter, 
        it will start all of his models, intervals and move on.
        """
        #print globals.NUMBERENEMIES,globals.NUM_MAX_NUMBERENEMIES
        self.actor.loadModel(self.model)

        #Enable transparency to the enemy
        #self.actor.setTransparency(1)
        #self.node.setTransparency(1)
        
        #Loading its destruction sound
        self.snd = loader.loadSfx(self.sound) 
        
        #Make it visible
        self.actor.setPos(self.startPos)
        self.actor.reparentTo(self.node)
        
        #Collisions
        #Create a collision solid for this model
        self.cNode = CollisionNode('enemyCol')
        #Setting "from" bitmask to 0
        self.cNode.setFromCollideMask(BitMask32.bit(0))
        self.cNode.addSolid(CollisionSphere(0, 0, 0, 1.0))
        self.cNodePath = self.actor.attachNewNode(self.cNode)
        #Show collision solidF
        self.cNodePath.show()

        self.actor.loadAnims({"idle":self.idle_anim})
        self.actor.loadAnims({"run":self.run_anim})
        self.actor.loadAnims({"attack":self.attack_anim})
        self.actor.loadAnims({"dead":self.dead_anim})
        self.actor.loadAnims({"hit":self.hit_anim})

        self.actor.bindAnim('run')
        self.actor.loop('run')
        self.actor.setPlayRate(1.5 + (globals.RATE - 1), 'run')
        
        self.node.setPos(self.startPos)
        self.actor.setColor(1.0,1.0,1.0,1.0)
        self.actor.setScale(self.scale)
        
        self.actor.setHpr(Vec3(self.H,self.P,self.R))
        #print self.actor.getHpr()
        self.mySeq = Sequence()

        j = 0
        for i in self.points:
            int = LerpPosInterval(self.node,self.times[j],i)
            self.mySeq.append(int)
            j += 1

        #Since we dont want enemies to "pop up" onto the screen
        #we create a small scale interval
        self.grow = LerpScaleInterval(self.node, 0.25, 1.0, 000.1)
        #We create then a parallel with grow to go along with the sequenece

        self.parallel = Parallel(self.grow,self.mySeq)

        if self.loop:
            self.parallel.loop()
        else:
            self.parallel.start()
        
        #base.cTrav.addCollider(self.cNodePath,globals.PhysicsBox)

        self.updateTask = taskMgr.add(self._update, 'Enemy-Update')
#
#        self.AIchar = AICharacter("seeker",self.model, 100, 10.5, 5)
#        globals.AIworld.addAiChar(self.AIchar)
#        self.AIbehaviors = self.AIchar.getAiBehaviors()
#
#        self.AIbehaviors.seek(globals.PlayerPosition)
#        #self.model.loop("run")

        return task.done



    def _update(self, task):
        """Task function

        This will update the state of the enemy
        """
        #Check if the parallel is still playing
        if not self.parallel.isPlaying():
            #If we are making fancy colors, we have to
            # be sure it was killed by the die function
            if self.dead == True:
                #Call nice particles and sound
                #self._particle(self.pos)
                #self.snd.play()
                #Update Score!!
                globals.SCORE += self.scorePoints


            globals.NUMBERENEMIES -= 1

            self.node.removeNode()
            #print "deleted:" , self
            del self
            return task.done


        return task.cont

    def die(self):
        self.dead = True
        #self.parallel.finish()

